package com.gitee.huxing.course.conc.conc03.collection;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc03.collection
 * @ClassName: CopyOnWriteArrayListDemo
 * @Author: huxingmale
 * @DateTime: 2021-08-25 21:50
 */
public class CopyOnWriteArrayListDemo {

    public static void main(String[] args) {

        List<Integer> list = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        T1 t1 = new T1(list);
        T2 t2 = new T2(list);
        t1.start();
        t2.start();
    }

    public static class T1 extends Thread {

        private List<Integer> list;

        public T1(List<Integer> list) {
            this.list = list;
        }

        public void run() {
            for (Integer i : list) {
                System.out.println(i);
            }
        }
    }

    public static class T2 extends Thread {

        private List<Integer> list;

        public T2(List<Integer> list) {
            this.list = list;
        }

        public void run() {
            for (int i = 0; i < list.size(); i++) {
                list.remove(i);
            }
        }
    }
}
